/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.util.handler;

import io.protostuff.LinkedBuffer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * Protostuff对象池工厂.
 *
 * @author zengdegui
 * @since 2025/11/01 02:07
 */
@Slf4j
@AllArgsConstructor
public class PooledProtostuffFactory extends BasePooledObjectFactory<LinkedBuffer> {

    private final Integer bufferSize;

    @Override
    public LinkedBuffer create() throws Exception {
        return LinkedBuffer.allocate(bufferSize);
    }

    @Override
    public PooledObject<LinkedBuffer> wrap(LinkedBuffer linkedBuffer) {
        return new DefaultPooledObject<>(linkedBuffer);
    }

    /**
     * 回收对象.
     *
     * @param p
     */
    @Override
    public void passivateObject(PooledObject<LinkedBuffer> p) {
        p.getObject().clear();
    }
}
